<template>
  <div class="rsrj-xrj-page">
    <el-card>
      <div slot="header">写日记</div>
      <el-form :model="form" label-width="60px" style="max-width: 600px;">
        <el-form-item label="标题">
          <el-input v-model="form.title" placeholder="请输入日记标题" />
        </el-form-item>
        <el-form-item label="内容">
          <div id="editor" style="border:1px solid #e4e7ed; border-radius:4px;"></div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handlePublish">发布</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import E from 'wangeditor';
export default {
  name: 'RsrjXrjView',
  data() {
    return {
      form: {
        title: '',
        content: ''
      },
      editor: null
    }
  },
  mounted() {
    this.editor = new E('#editor');
    this.editor.config.onchange = html => {
      this.form.content = html;
    };
    this.editor.create();
  },
  beforeDestroy() {
    if (this.editor) this.editor.destroy();
  },
  methods: {
    handlePublish() {
      if (!this.form.title || !this.form.content) {
        this.$message.warning('请填写标题和内容');
        return;
      }
      this.$message.success('发布成功');
      this.form.title = '';
      this.form.content = '';
      this.editor.txt.clear();
    }
  }
}
</script>
<style scoped>
.rsrj-xrj-page .el-card { box-shadow: 0 2px 6px rgba(74,158,255,0.04) !important; }
</style> 